﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using EncircleFTM;

namespace Scheduler
{
    static class Program
    {
        /// <summary>
        /// Timer control 
        /// </summary>
        private static System.Timers.Timer timerScheduler;
        private static bool isStarted = false;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            EncircleFTM.clsSettings.ReadSettings();
            Application.Run(new ClearLogscheduler());
            //return;

            string command = string.Empty;
            while (true)
            {               
                if(args !=null && args.Length>0)
                    command = args[0];
                
                switch(command.ToUpper())
                {
                    case "START":
                        if (!isStarted)
                        {
                            InitSchedular();
                        }
                        break;
                    case "STOP":
                        stop();
                        return;
                        break;
                }
                args = new string[] { };

                Application.DoEvents();

                System.GC.Collect();
            }
        }

        /// <summary>
        /// Intializing the schedular and assign timer interval
        /// </summary>
        public static void InitSchedular()
        {
            try
            {
                timerScheduler = new System.Timers.Timer();
                timerScheduler.Elapsed += new System.Timers.ElapsedEventHandler(timerScheduler_Elapsed);

                //Read Settings parameter "LastUpdatedDate" and "ScheduleType"  
                DateTime lastUpdateDate = EncircleFTM.clsSettings.lastClearedDate;
                int Day = EncircleFTM.clsSettings.lastClearDay;
                int Month = EncircleFTM.clsSettings.lastClearMonth;
                int Year = EncircleFTM.clsSettings.lastClearYear;
                int Hour = EncircleFTM.clsSettings.lastClearHour;
                int Min = EncircleFTM.clsSettings.lastClearMin;
                int Sec = EncircleFTM.clsSettings.lastClearSec;

                EncircleFTM.clsCommonData.ClearLogSchedule scheduleType = EncircleFTM.clsSettings.objClearLogSchedule;

                //calculate the scheduled time is reached or not by using method clsCommonData.SchedulerClearLog method
                EncircleFTM.clsCommonData.SchedulerStatus schedularStatus =
                     EncircleFTM.clsCommonData.SchedulerClearLog(scheduleType, Day, Month, Year, Hour, Min, Sec);

                if (schedularStatus.isReached)
                {
                    //if isReached the shceduled time clear the log  (method:ClearLog)
                    EncircleFTM.clsCommonData.ClearLog();

                    //finally update the last clear log date using (method:clsSettings.UpdateClearLogDate)
                    if (EncircleFTM.clsSettings.UpdateClearLogDate(DateTime.Now.Day,DateTime.Now.Month,DateTime.Now.Year,
                        DateTime.Now.Hour,DateTime.Now.Minute,DateTime.Now.Second))

                        timerScheduler.Interval = schedularStatus.ElapsedInterval;
                }
                else
                {
                    //the interval calculation will be return by the method SchedulerClearLog
                    if (schedularStatus.ElapsedInterval > 1)
                        timerScheduler.Interval = schedularStatus.ElapsedInterval;
                    else
                        timerScheduler.Interval = 1;
                }
                if (schedularStatus.ElapsedInterval > 1)
                    Start();
                else
                    stop();
            }
            catch (Exception ex)
            {
                //write to error log
                EncircleFTM.clsCommonData.WriteError(ex.Message);
            }
        }


        /// <summary>
        /// Used to handle timer elapsed handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void timerScheduler_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //Read Settings parameter "LastUpdatedDate" and "ScheduleType"  
            DateTime lastUpdateDate = EncircleFTM.clsSettings.lastClearedDate;
            EncircleFTM.clsCommonData.ClearLogSchedule scheduleType = EncircleFTM.clsSettings.objClearLogSchedule;
            int Day = EncircleFTM.clsSettings.lastClearDay;
            int Month = EncircleFTM.clsSettings.lastClearMonth;
            int Year = EncircleFTM.clsSettings.lastClearYear;
            int Hour = EncircleFTM.clsSettings.lastClearHour;
            int Min = EncircleFTM.clsSettings.lastClearMin;
            int Sec = EncircleFTM.clsSettings.lastClearSec;

            //calculate the scheduled time is reached or not by using method clsCommonData.SchedulerClearLog method
            EncircleFTM.clsCommonData.SchedulerStatus schedularStatus = EncircleFTM.clsCommonData.SchedulerClearLog(scheduleType,
                Day,Month,Year,Hour,Min,Sec);
               
            //if isReached the shceduled time clear the log  (method:ClearLog)
            EncircleFTM.clsCommonData.ClearLog();

            //finally update the last clear log date using (method:clsSettings.UpdateClearLogDate)
            if (EncircleFTM.clsSettings.UpdateClearLogDate(DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year,
                DateTime.Now.Hour, DateTime.Now.Minute,DateTime.Now.Second))

            timerScheduler.Interval = schedularStatus.ElapsedInterval;
        }


        /// <summary>
        /// Used to start the timer control
        /// </summary>
        public static void Start()
        {
            timerScheduler.Enabled = true;
            isStarted = true;
        }

        /// <summary>
        /// Used to stop the timer control
        /// </summary>
        public static void stop()
        {
            timerScheduler.Enabled = false;
            isStarted = false;
        }

        
    }
}
